#include "MyThread.h"
#include <QDebug>

MyThread::MyThread()
{
    m_timer = new QTimer();

    m_timer->moveToThread(this); //移动到MyThread线程
    connect(m_timer, &QTimer::timeout, this, &MyThread::OnTimeout, Qt::DirectConnection); //QueuedConnection不可以，会在主线程中调用

    qDebug() << "MyThread::MyThread: " << this->currentThreadId(); //MyThread自身和run是两个线程
}

MyThread::~MyThread()
{

}

void MyThread::run()
{
    qDebug() << "MyThread::run: " << this->currentThreadId();


    m_timer->start(2000);

    //进入事件循环, 不退出
    exec();
}

void MyThread::OnTimeout()
{
    qDebug() << "MyThread::OnTimeout: " << this->currentThreadId();
}
